#ifndef __thread_sync_hpp__
#define __thread_sync_hpp__

#include <boost/thread/thread.hpp>
#include <boost/thread/condition_variable.hpp>
#include <boost/exception/detail/exception_ptr.hpp>

namespace croco
{
namespace os
{

class thread_sync
{
public:
    /// thread creator interface
    thread_sync();
    void set_thread(boost::thread& t);
    void wait_for_start() const;
    void join();

    /// worker-thread interface
    void signal_started(const boost::exception_ptr& e = boost::exception_ptr());
    void set_error(const boost::exception_ptr& e);

private:
    boost::thread m_thread;
    bool m_started;
    mutable boost::mutex m_startSentinel;
    mutable boost::condition_variable m_startCV;
    boost::exception_ptr m_exc;
};

} // namespace os
} // namespace croco

#endif // __thread_sync_hpp__
